const ObjectCreator = {
    objects: {},

    init(scene) {
        this.createRoom(scene);
        this.createChair(scene);
        this.createPerson(scene);
        this.createFlashlightPerson(scene);
    },

    getObjects() {
        return this.objects;
    },

    createRoom(scene) {
        const roomGroup = new THREE.Group();

        // 地板
        const floorGeometry = new THREE.PlaneGeometry(8, 6);
        const floorMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
        const floor = new THREE.Mesh(floorGeometry, floorMaterial);
        floor.rotation.x = -Math.PI / 2;
        floor.receiveShadow = true;
        roomGroup.add(floor);

        // 墙壁
        const wallMaterial = new THREE.MeshLambertMaterial({ color: 0xE6E6FA });

        // 后墙
        const backWallGeometry = new THREE.PlaneGeometry(8, 4);
        const backWall = new THREE.Mesh(backWallGeometry, wallMaterial);
        backWall.position.set(0, 2, -3);
        backWall.receiveShadow = true;
        roomGroup.add(backWall);

        // 左墙
        const leftWallGeometry = new THREE.PlaneGeometry(6, 4);
        const leftWall = new THREE.Mesh(leftWallGeometry, wallMaterial);
        leftWall.position.set(-4, 2, 0);
        leftWall.rotation.y = Math.PI / 2;
        leftWall.receiveShadow = true;
        roomGroup.add(leftWall);

        // 右墙
        const rightWall = new THREE.Mesh(leftWallGeometry, wallMaterial);
        rightWall.position.set(4, 2, 0);
        rightWall.rotation.y = -Math.PI / 2;
        rightWall.receiveShadow = true;
        roomGroup.add(rightWall);

        // 天花板
        const ceilingGeometry = new THREE.PlaneGeometry(8, 6);
        const ceilingMaterial = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
        const ceiling = new THREE.Mesh(ceilingGeometry, ceilingMaterial);
        ceiling.position.y = 4;
        ceiling.rotation.x = Math.PI / 2;
        ceiling.receiveShadow = true;
        roomGroup.add(ceiling);

        // 添加简单的灯泡装饰
        const bulbGeometry = new THREE.SphereGeometry(0.1, 8, 6);
        const bulbMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
        const bulb = new THREE.Mesh(bulbGeometry, bulbMaterial);
        bulb.position.set(0, 3.8, 0);
        roomGroup.add(bulb);

        scene.add(roomGroup);
        this.objects.room = roomGroup;
    },

    createChair(scene) {
        const chairGroup = new THREE.Group();
        const chairMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });

        // 椅座
        const seatGeometry = new THREE.BoxGeometry(0.8, 0.1, 0.8);
        const seat = new THREE.Mesh(seatGeometry, chairMaterial);
        seat.position.y = 0.8;
        seat.castShadow = true;
        chairGroup.add(seat);

        // 椅腿
        const legGeometry = new THREE.BoxGeometry(0.05, 0.8, 0.05);
        for (let i = 0; i < 4; i++) {
            const leg = new THREE.Mesh(legGeometry, chairMaterial);
            const x = i < 2 ? -0.35 : 0.35;
            const z = i % 2 === 0 ? -0.35 : 0.35;
            leg.position.set(x, 0.4, z);
            leg.castShadow = true;
            chairGroup.add(leg);
        }

        // 椅背
        const backGeometry = new THREE.BoxGeometry(0.8, 0.6, 0.05);
        const back = new THREE.Mesh(backGeometry, chairMaterial);
        back.position.set(0, 1.2, -0.375);
        back.castShadow = true;
        chairGroup.add(back);

        // 让椅子旋转180度
        chairGroup.rotation.y = Math.PI;

        chairGroup.position.set(0, 0, 0);
        scene.add(chairGroup);
        this.objects.chair = chairGroup;
    },

    createPerson(scene) {
        const personGroup = new THREE.Group();

        // 身体
        const bodyGeometry = new THREE.BoxGeometry(0.4, 0.8, 0.2);
        const bodyMaterial = new THREE.MeshLambertMaterial({ color: 0x4169E1 });
        const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
        body.position.y = 1.3;
        body.castShadow = true;
        personGroup.add(body);

        // 头部
        const headGeometry = new THREE.SphereGeometry(0.15, 8, 6);
        const headMaterial = new THREE.MeshLambertMaterial({ color: 0xFFDBB5 });
        const head = new THREE.Mesh(headGeometry, headMaterial);
        head.position.y = 1.85;
        head.castShadow = true;
        personGroup.add(head);

        // 手臂
        const armGeometry = new THREE.BoxGeometry(0.1, 0.5, 0.1);
        const armMaterial = new THREE.MeshLambertMaterial({ color: 0xFFDBB5 });

        const leftArm = new THREE.Mesh(armGeometry, armMaterial);
        leftArm.position.set(-0.3, 1.2, 0);
        leftArm.castShadow = true;
        personGroup.add(leftArm);

        const rightArm = new THREE.Mesh(armGeometry, armMaterial);
        rightArm.position.set(0.3, 1.2, 0);
        rightArm.castShadow = true;
        personGroup.add(rightArm);

        // 腿部
        const legGeometry = new THREE.BoxGeometry(0.15, 0.6, 0.15);
        const legMaterial = new THREE.MeshLambertMaterial({ color: 0x000080 });

        const leftLeg = new THREE.Mesh(legGeometry, legMaterial);
        leftLeg.position.set(-0.1, 0.6, 0.1);
        leftLeg.castShadow = true;
        personGroup.add(leftLeg);

        const rightLeg = new THREE.Mesh(legGeometry, legMaterial);
        rightLeg.position.set(0.1, 0.6, 0.1);
        rightLeg.castShadow = true;
        personGroup.add(rightLeg);

        personGroup.position.set(0, -0.15, 0);
        scene.add(personGroup);
        this.objects.person = personGroup;
    },

    createFlashlightPerson(scene) {
        const personGroup = new THREE.Group();

        // 定义目标位置（坐着的人的头部位置）
        const targetPosition = new THREE.Vector3(0, 1.85, 0.1);
        // 手电筒位置（站立的人的手部位置）
        const flashlightPosition = new THREE.Vector3(0.3, 1.5, -1.5);

        // 身体
        const bodyGeometry = new THREE.BoxGeometry(0.35, 0.7, 0.18);
        const bodyMaterial = new THREE.MeshLambertMaterial({ color: 0x228B22 });
        const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
        body.position.y = 1.1;
        body.castShadow = true;
        personGroup.add(body);

        // 头部
        const headGeometry = new THREE.SphereGeometry(0.13, 8, 6);
        const headMaterial = new THREE.MeshLambertMaterial({ color: 0xFFDBB5 });
        const head = new THREE.Mesh(headGeometry, headMaterial);
        head.position.y = 1.6;
        head.castShadow = true;
        personGroup.add(head);

        // 左手臂
        const leftArmGeometry = new THREE.BoxGeometry(0.08, 0.4, 0.08);
        const armMaterial = new THREE.MeshLambertMaterial({ color: 0xFFDBB5 });
        const leftArm = new THREE.Mesh(leftArmGeometry, armMaterial);
        leftArm.position.set(-0.25, 1.0, 0);
        leftArm.castShadow = true;
        personGroup.add(leftArm);

        // 计算手臂指向目标的角度
        const armDirection = new THREE.Vector3().subVectors(targetPosition, flashlightPosition).normalize();
        const armAngleY = Math.atan2(armDirection.x, armDirection.z);
        const armAngleX = Math.asin(-armDirection.y);

        // 右手臂（持手电筒）- 调整指向目标
        const rightArm = new THREE.Mesh(leftArmGeometry, armMaterial);
        rightArm.position.set(0.25, 1.2, 0);
        rightArm.rotation.x = armAngleX;
        rightArm.rotation.y = armAngleY * 0.3; // 稍微调整角度让姿势更自然
        rightArm.rotation.z = -0.2;
        rightArm.castShadow = true;
        personGroup.add(rightArm);

        // 手电筒组
        const flashlightGroup = new THREE.Group();

        // 手电筒主体
        const flashlightBodyGeometry = new THREE.CylinderGeometry(0.03, 0.03, 0.2, 8);
        const flashlightBodyMaterial = new THREE.MeshLambertMaterial({ color: 0x333333 });
        const flashlightBody = new THREE.Mesh(flashlightBodyGeometry, flashlightBodyMaterial);
        flashlightBody.castShadow = true;
        flashlightGroup.add(flashlightBody);

        // 手电筒头部
        const flashlightHeadGeometry = new THREE.CylinderGeometry(0.04, 0.03, 0.05, 8);
        const flashlightHeadMaterial = new THREE.MeshLambertMaterial({ color: 0x666666 });
        const flashlightHead = new THREE.Mesh(flashlightHeadGeometry, flashlightHeadMaterial);
        flashlightHead.position.y = 0.125;
        flashlightHead.castShadow = true;
        flashlightGroup.add(flashlightHead);

        // 手电筒灯泡
        const bulbGeometry = new THREE.SphereGeometry(0.02, 6, 4);
        const bulbMaterial = new THREE.MeshBasicMaterial({ color: 0xffff99 });
        const bulb = new THREE.Mesh(bulbGeometry, bulbMaterial);
        bulb.position.y = 0.15;
        flashlightGroup.add(bulb);

        // 计算手电筒指向目标的方向
        const direction = new THREE.Vector3().subVectors(targetPosition, flashlightPosition).normalize();

        // 计算旋转角度让手电筒指向目标
        const angleY = Math.atan2(direction.x, direction.z);
        const angleX = Math.asin(-direction.y);

        // 设置手电筒位置和旋转
        flashlightGroup.position.set(0.35, 1.4, 0);

        // 先让手电筒指向正确方向
        flashlightGroup.rotation.y = angleY;
        flashlightGroup.rotation.x = angleX;
        // 稍微调整Z轴旋转让手电筒在手中的角度更自然
        flashlightGroup.rotation.z = -0.2;

        personGroup.add(flashlightGroup);

        // 腿部
        const legGeometry = new THREE.BoxGeometry(0.12, 0.8, 0.12);
        const legMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });

        const leftLeg = new THREE.Mesh(legGeometry, legMaterial);
        leftLeg.position.set(-0.08, 0.4, 0);
        leftLeg.castShadow = true;
        personGroup.add(leftLeg);

        const rightLeg = new THREE.Mesh(legGeometry, legMaterial);
        rightLeg.position.set(0.08, 0.4, 0);
        rightLeg.castShadow = true;
        personGroup.add(rightLeg);

        // 位置在凳子前方
        personGroup.position.set(0, 0, -1.5);
        scene.add(personGroup);
        this.objects.flashlightPerson = personGroup;
    }
};